const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    codeText: '获取验证码',
    second: 60, //初始时间
    canClick: true,
    interval: '', //定时器
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            isShow: true
          });
        }
      }
    });
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      app.globalData.userInfo = e.detail.userInfo;
      this.setData({
        isShow: false
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) { }
      });
    }
  },
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputCode: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  resetTap: function() {
    clearInterval(this.data.interval);
    this.setData({
      canClick: true,
      second: 60,
      interval: '',
      codeText: '获取验证码'
    });
  },
  /**
   * 开始倒计时
   */
  startTap: function() {
    var that = this;
    that.init(that);
    var second = that.data.second;
    var interval = setInterval(function() {
      second--;
      that.setData({
        second: second,
        codeText: '请重试(' + second + 's)',
        canClick: false
      })
      if (second == 0) { //归0时回到60
        that.resetTap();
      }
    }, 1000)
    that.setData({
      interval: interval,
      codeText: '请重试(' + second + 's)',
      canClick: false
    })
  },
  /**
   * 清除interval
   * @param that
   */
  clearTimeInterval: function(that) {
    var interval = that.data.interval;
    clearInterval(interval);
  },
  /**
   * 初始化数据
   */
  init: function(that) {
    var second = 60;
    var interval = '';
    that.clearTimeInterval(that)
    that.setData({
      canClick: true,
      second: second,
      interval: interval,
    })
  },
  getCode: function() {
    const phone = this.data.phone;
    if (phone.length === 0) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    this.setData({
      canClick: true,
    });
    this.startTap();
    const url = app.globalData.url;
    wx.request({
      url: url + '/management/traineeCode/' + phone,
      success: function(response) {
        if (response.data.success) {
          wx.showToast({
            title: '验证码已经发送,请注意查看',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '提取验证码失败',
            icon: 'none'
          });
        }
      }
    })
  },
  registration: function() {
    if (this.data.phone.length === 0) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    if (this.data.code.length === 0) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }
    const userInfo = app.globalData.userInfo;
    const openId = app.globalData.openId;
    const phone = this.data.phone;
    const code = this.data.code;
    const url = app.globalData.url;
    wx.request({
      url: url + '/coach/registration',
      data: {
        openId: openId,
        avatarUrl: userInfo.avatarUrl,
        nickName: userInfo.nickName,
        phone: phone,
        code: code
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success === true) {
          wx.navigateBack({
            delta: 1
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面卸载
   * 退出本页面时停止计时器
   */
  onUnload: function() {
    var that = this;
    that.clearTimeInterval(that)
  },

  /**
   * 生命周期函数--监听页面隐藏
   * 在后台运行时停止计时器
   */
  onHide: function() {
    var that = this;
    that.clearTimeInterval(that)
  }
})